//
//  XDMgrComplainViewController.m
//  XDEducation
//
//  Created by apple on 2017/5/4.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDMgrComplainViewController.h"
#import "XDMgrComplainCell.h"
#import "HNComplainModel.h"
#import "HNMgrComplainController.h"

@interface XDMgrComplainViewController ()
@property (nonatomic, assign) NSInteger Index;
@property(nonatomic,strong) NSMutableArray *DataArr;
@end

static NSString *cellid = @"MgrComplainCell";

@implementation XDMgrComplainViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.Index = 1;
    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"XDMgrComplainCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellid];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self RefreshData];
    }];
    
    [self.tableView.mj_footer beginRefreshing];
}


#pragma mark - 网络请求
- (void)RefreshData
{
    NSDictionary *Dict = @{
                           @"uid":[AccountTool shareAccountTool].currentAccount.uid,
                           @"pageNum":@(self.Index)
                           };
    [XDHttpTool get:BaseURL(@"user.do?action=getAllWaitingPromiseAppeal") authCode:[AccountTool shareAccountTool].currentAccount.authCode params:Dict success:^(id responseObj) {
        
        [self EndRefresh];
        if ([responseObj[@"code"] isEqualToNumber:@1000])
        {
            if (self.DataArr.count > 0 && [responseObj[@"t"][@"dataList"] count] == 0)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView.mj_footer endRefreshingWithNoMoreData];
                });
            }
            else
            {
                for (NSDictionary *Dict in responseObj[@"t"][@"dataList"])
                {
                    [self.DataArr addObject:[[HNComplainModel alloc] init_HNModelWithDict:Dict]];
                }
                self.Index += 1;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            }
        }
        else
        {
            [MBProgressHUD showError:responseObj[@"msg"]];
        }
    } failure:^(NSError *error) {
        
        [self EndRefresh];
        [MBProgressHUD showError:XDNetWorkError];
    }];
}


- (void)EndRefresh
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    });
}



#pragma mark - Action
- (void)GoHNMgrComplainControllerVCWithID:(NSNumber *)ID Content:(NSString *)Content
{
    HNMgrComplainController *VC = [[HNMgrComplainController alloc] init];
    VC.ID = ID;
    VC.Content = Content;
    VC.title = @"处理申诉";
    [self.navigationController pushViewController:VC animated:YES];
    VC.AnswerOk = ^()
    {
        
        [self.DataArr removeAllObjects];
        [self.tableView reloadData];
        [self.tableView.mj_footer beginRefreshing];
};
}


#pragma mark - TableView代理

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.DataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 330;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XDMgrComplainCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
    HNComplainModel *Model = self.DataArr[indexPath.row];
    cell.Model = Model;
    cell.ClickMgr = ^(NSNumber *ID,NSString *Content)
    {
        [self GoHNMgrComplainControllerVCWithID:ID Content:Content];
    };
    return cell;
}



#pragma mark - Setter
- (NSMutableArray *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = [NSMutableArray array];
    }
    return _DataArr;
}

@end
